﻿/*************************************************************************************
* 运行库版本：	4.0.30319.18408
* 类  名  称：	LightNetworkInformation
* 命 名 空间：	IOTILMS.Business
* 文  件  名：	LightNetworkInformation
* 创 建 时间：	2014/5/15 0:59:08
* 作      者：	帅红  shuaihong617@qq.com
* 说      明：
* 修 改 时间：
* 修  改  人：
*************************************************************************************/

using System.Net;
using FutureTech.ComponentModel;

namespace FutureTech.Network
{
        /// <summary>
        /// 
        /// </summary>
        public class AddressContext : NotifyPropertyChangedObject
        {
                

                /// <summary>
                /// Initializes a new instance of the <see cref="AddressContext"/> class.
                /// </summary>
                /// <param name="id">The unique identifier.</param>
                /// <param name="ipEndPoint">The ip end point.</param>
                public AddressContext(string id, IPEndPoint ipEndPoint)
                {
                        Id = id;
                        IpEndPoint = ipEndPoint;
                        
                }

                /// <summary>
                /// Initializes a new instance of the <see cref="AddressContext"/> class.
                /// </summary>
                /// <param name="id">The unique identifier.</param>
                public AddressContext(string id)
                        :this(id, new IPEndPoint(IPAddress.Loopback, 0))
                {

                }

                /// <summary>
                /// Gets the unique identifier.
                /// </summary>
                /// <value>
                /// The unique identifier.
                /// </value>
                public string Id { get; private set; }


                private IPEndPoint _ipEndPoint;

                /// <summary>
                /// Gets or sets the ip end point.
                /// </summary>
                /// <value>
                /// The ip end point.
                /// </value>
                public IPEndPoint IpEndPoint
                {
                        get { return _ipEndPoint; }
                        set
                        {
                                _ipEndPoint = value;
                                this.NotifyPropertyChanged(i => i.IpEndPoint);
                        }
                }

                private int _ipResetTimes;

                /// <summary>
                /// IP重置次数
                /// </summary>
                /// <value>
                /// The reconnect times.
                /// </value>
                public int IpResetTimes
                {
                        get { return _ipResetTimes; }
                        set
                        {
                                _ipResetTimes = value;
                                this.NotifyPropertyChanged(i => i.IpResetTimes);
                        }
                }

                private int _natResetTimes;

                /// <summary>
                /// NAT线路重置次数，IP重新分配时值不变
                /// </summary>
                /// <value>
                /// The reconnect times.
                /// </value>
                public int NatResetTimes
                {
                        get { return _natResetTimes; }
                        set
                        {
                                _natResetTimes = value;
                                this.NotifyPropertyChanged(i => i.NatResetTimes);
                        }
                }


                /// <summary>
                /// Updates the specified ip end point.
                /// </summary>
                /// <param name="ipEndPoint">The ip end point.</param>
                public void Update(IPEndPoint ipEndPoint)
                {
                        if (!IpEndPoint.Address.Equals(ipEndPoint.Address))
                        {
                                IpResetTimes++;
                        }
                        else
                        {
                                if (IpEndPoint.Port != ipEndPoint.Port)
                                {
                                        NatResetTimes++;
                                }
                        }
                        
                        IpEndPoint = ipEndPoint;
                }
               
        }
}
